package org.example.Frame;

import org.example.Util.FileUtil;
import org.example.Pojo.User;
import org.example.implement.MyFrame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Map;

public class register extends JFrame implements MyFrame {
    //变量
    private JTextField textFieldUsername;
    private JPasswordField passwordFieldPassword;
    private JPasswordField passwordFieldConfirmPassword;
    private JLabel lblUsername;
    private JLabel lblPassword;
    private JLabel lblConfirmPassword;
    private JButton btnRegister;
    private Map<String,String> map;

    //这里也需要一个哈希表对文件进行读取
    public register(Map<String,String> map) throws HeadlessException {
        //初始化界面
        this.setTitle("注册界面");
        this.setSize(450,300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel=new JPanel();
        this.add(panel);
        draw(panel);
        this.setVisible(true);
        //初始化数据
        this.map=map;
    }

    //绘制界面
    @Override
    public void draw(JPanel panel){
        panel.setLayout(null);

        lblUsername = new JLabel("用户名：");//用户名标签
        lblUsername.setBounds(50, 50, 80, 25);
        panel.add(lblUsername);

        textFieldUsername = new JTextField();//用户名填写
        textFieldUsername.setBounds(150, 50, 160, 25);
        textFieldUsername.setColumns(10);
        panel.add(textFieldUsername);

        lblPassword = new JLabel("密码：");//密码标签
        lblPassword.setBounds(50, 100, 80, 25);
        panel.add(lblPassword);

        passwordFieldPassword = new JPasswordField();//密码填写
        passwordFieldPassword.setBounds(150, 100, 160, 25);
        panel.add(passwordFieldPassword);

        lblConfirmPassword = new JLabel("确认密码：");//确定密码标签
        lblConfirmPassword.setBounds(50, 150, 80, 25);
        panel.add(lblConfirmPassword);

        passwordFieldConfirmPassword = new JPasswordField();//确定密码填写
        passwordFieldConfirmPassword.setBounds(150, 150, 160, 25);
        panel.add(passwordFieldConfirmPassword);

        btnRegister = new JButton("注册");//注册按钮

        //点击注册按钮
        btnRegister.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String username = textFieldUsername.getText();
                String password = new String(passwordFieldPassword.getPassword());
                String confirmPassword = new String(passwordFieldConfirmPassword.getPassword());
                //用户存在则失败
                //密码和确认密码不一样则失败
                //三个状态栏有一项为空则失败
                if(map.containsKey(textFieldUsername.getText())){
                    JOptionPane.showMessageDialog(new JOptionPane(),"用户已存在,若忘记密码请联系管理员","警告",JOptionPane.WARNING_MESSAGE);
                }
                else if(!password.equals(confirmPassword)){
                    JOptionPane.showMessageDialog(new JOptionPane(), "密码和确认密码不匹配，请重新输入！", "警告", JOptionPane.WARNING_MESSAGE);
                }
                else if(username.isEmpty()||password.isEmpty()){
                    JOptionPane.showMessageDialog(new JOptionPane(),"用户名或者密码为空，请重新输入","警告",JOptionPane.WARNING_MESSAGE);
                }
                //都满足则创建用户文件用来保存信息
                else {
                    try {
                        new FileUtil().CreateFile(new User(username, password));
                        System.out.println("注册用户成功");
                        close();
                    } catch (IOException ex) {
                        throw new RuntimeException(ex);
                    }
                }
            }
        });

        btnRegister.setBounds(150, 200, 100, 25);
        panel.add(btnRegister);
    }

    //关闭界面重新打开登录界面
    @Override
    public void close(){
        this.setVisible(false);
        new login().setVisible(true);
    }
}
